package com.example.kliven.because.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.kliven.because.R;
import com.example.kliven.because.adapter.RecommendListViewAdapter;
import com.example.kliven.because.bean.RecommendBean;
import com.example.kliven.because.intentbean.NetRecommendBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class RecommendActivity extends Activity implements View.OnClickListener {

    private RequestQueue queue;
    private String url = "http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_member_ranking&parameters=%7B%22_member_id%22:%22289167%22,%22_start%22:%220%22,%22_end%22:%220%22%7D&memberId=289167";
//    private String imgUrl = "http://ywimg.iyinwei.com/Content/Images/yinwei/upload/appImage/20151116/20151116134141_918.jpg";

    private RecommendListViewAdapter adapter;
    private List<RecommendBean> recommendBeans;
    private NetworkImageView img_main;
    private ListView listView;
    private Button btn_go_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommend);
        listView = (ListView) findViewById(R.id.list_view);
        img_main = (NetworkImageView) findViewById(R.id.iv_img1);
//        getNetImg();
//        btn_go_back.setOnClickListener(this);
        getNetInfo();
    }


    private void getNetInfo() {
        queue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                recommendBeans = new ArrayList<>();
                Gson gson = new Gson();
                NetRecommendBean netRecommendBean = gson.fromJson(response, NetRecommendBean.class);

                for (int i = 0; i < netRecommendBean.getData().getMember().size(); i++) {
                    RecommendBean recommendbean = new RecommendBean();
                    recommendbean.setMember_level_name(netRecommendBean.getData().getMember().get(i).getMember_level_name());
                    String member_name = netRecommendBean.getData().getMember().get(i).getMember_name();
                    recommendbean.setMember_name(member_name);
                    recommendbean.setAward_sum(netRecommendBean.getData().getMember().get(i).getAward_sum());
                    recommendbean.setShare_sum(netRecommendBean.getData().getMember().get(i).getShare_sum());
                    recommendbean.setIco(netRecommendBean.getData().getMember().get(i).getIco());

                    String img = netRecommendBean.getData().getMember().get(i).getPhoto_url_list();
                    String imgMain = img.substring(0, img.indexOf(","));
                    Log.d("*************", imgMain);
                    recommendbean.setMain_img(imgMain);

                    recommendBeans.add(recommendbean);

                }
                adapter = new RecommendListViewAdapter(recommendBeans, RecommendActivity.this);
                listView.setAdapter(adapter);
                Log.d("RecommendActivity", netRecommendBean.getData().getMember().get(0).getLevel());
                Log.d("RecommendActivity", response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("RecommendActivity", "error:" + error);
            }
        });
        queue.add(request);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.iv_go_back:
//                Intent intent = new Intent(RecommendActivity.this, HomePageAttentionFragment.class);
//                startActivity(intent);
//                break;
        }
    }
}
